<?php
namespace app\admin\controller;

use app\common\controller\AdminBase;
use app\common\model\Csuser as CsuserModel;
use think\Session;
use think\Config;

class Cservice extends AdminBase
{
    protected $csuser_model;
    protected $wechat_info;
    protected function _initialize(){
        parent::_initialize();
        $this->csuser_model = new CsuserModel;
        $this->wechat_info = Session::get('wechat_info');
    }

    public function index($keyword = ''){
        $csuser = $this->csuser_model->where('auid',$this->wechat_info['id'])->select();
        $this->assign('csuser',$csuser);
        return $this->fetch('index',[
            'keyword'=>$keyword,
        ]);
    }

    public function add(){
        return $this->fetch();
    }

    public function save(){
        if ($this->request->isPost()) {
            $data            = $this->request->post();
            // $validate_result = $this->validate($data, 'User');

            // if ($validate_result !== true) {
            //     $this->error($validate_result);
            // } else {
                $wechat_info = $this->wechat_info;
                $data['auid'] = $wechat_info['id'];
                $data['password'] = md5($data['password'] . Config::get('salt'));
                if ($this->csuser_model->allowField(true)->save($data)) {
                    $this->success('保存成功');
                } else {
                    $this->error('保存失败');
                }
            // }
        }
    }

    public function edit(){
        return $this->fetch();
    }

    public function update(){

    }

    public function delete(){

    }
}